package com.zlll.winner.controller.media;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.media.MediaVideoList;
import com.zlll.winner.business.service.media.IMediaVideoListService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.common.BaseResult;
import com.zlll.winner.enums.OperationEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 视频栏目管理
 */
@LoginUser
@RestController
@RequestMapping(value = "/media/video/list")
public class MediaVideoController extends BaseController {

    @Reference
    private IMediaVideoListService iMediaVideoListService;

    /**
     * 分页列表
     * @param params
     * @return
     */
    @PostMapping(value = "page")
    public Object page(@RequestBody JSONObject params){
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        Assert.notNull(pageNo,"pageNo不能为空");
        Assert.notNull(pageSize,"pageSize不能为空");
        return iMediaVideoListService.fetchVideoListPage(params);
    }

    /**
     * 新增视频
     * @param data
     * @return
     */
    @PostMapping("add")
    public BaseResult addVideo(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        MediaVideoList mediaVideoList = data.toJavaObject(MediaVideoList.class);
        Assert.notNull(mediaVideoList.getMenuId(),"栏目Id不能为空");
        boolean result = iMediaVideoListService.saveVideo(mediaVideoList, getUser());
        if (result) {
            baseResult.setMsg("新增主持人失败");
            baseResult.setSuccess(false);
            return baseResult;
        }
        return baseResult;
    }

    /**
     * 编辑功能
     * @param data
     * @return
     */
    @PostMapping("edit")
    public BaseResult editVideo(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        MediaVideoList mediaVideoList = data.toJavaObject(MediaVideoList.class);
        Assert.notNull(mediaVideoList.getMenuId(),"栏目Id不能为空");
        Assert.notNull(mediaVideoList.getId(),"Id不能为空");
        iMediaVideoListService.editVideo(mediaVideoList, getUser());
        return baseResult;
    }

    /**
     * 删除
     * @param data
     * @return
     */
    @PostMapping("delete")
    public BaseResult updateVideo(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long id = data.getLong("id");
        Assert.notNull(id,"视频Id不能为空");
        String operation = data.getString("operation");
        if (OperationEnum.DELETED.getValue().equals(operation)) {
            int result = iMediaVideoListService.deleteVideoById(id, getUser().getName());
            if (result == 0) {
                baseResult.setMsg("视频删除失败,当前视频id为：" + id);
                baseResult.setSuccess(false);
                return baseResult;
            }
            return baseResult;
        }
        if (OperationEnum.OBTAINED.getValue().equals(operation)) {
            int result = iMediaVideoListService.updateVideoStatusById(id, getUser().getName());
            if (result == 0) {
                baseResult.setMsg("视频下架失败,当前视频id为：" + id);
                baseResult.setSuccess(false);
                return baseResult;
            }
            return baseResult;
        }
        baseResult.setMsg("参数operation的值需为delete 或者 obtained");
        baseResult.setSuccess(false);
        return baseResult;
    }
}
